package tech.abing.spider.task.pageprocessor;

import java.util.List;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.StartContext;

public class ProductIDProccessor1 implements PageProcessor {

	static Pattern normalContain = Pattern.compile("天猫");//正常页面必定会存在的字符，你也可以使用页面上其他的字符
	
	@Override
	public PageScript getJavaScript() {
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		return normalContain;
	}

	@Override
	public void process(OkPage page, StartContext context, List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		
		 Document doc = Jsoup.parse(page.getContent());
		 
		 //==>/i/asynSearch.htm?mid=w-8210437040-0&wid=8210437040&path=/search.htm&&amp;search=y&amp;spm=a1z10.5-b.w4011-8210412463.99.FsCwuu&amp;scene=taobao_shop&amp;orderType=hotsell_desc&amp;tsearch=y
		 String distUrl = doc.select("input[id=J_ShopAsynSearchURL]").attr("value");
		 
		 String url2 = "https://shop"+page.getRequest().getParamsByName("shopId")+".taobao.com" + distUrl;
		 
		 PageRequest req = context.createPageRequest(url2, ProductIDProccessor2.class, 1);
		 req.putParams("shopId", page.getRequest().getParamsByName("shopId").toString());
		 req.putParams("saleCount", page.getRequest().getParamsByName("saleCount").toString());
		 req.putParams("type", page.getRequest().getParamsByName("type").toString());
		 queue.add(req);//跟进队列

	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}


}
